<template>
  <div class="app-container">
    <!--工具栏-->
    <div v-if="!showComponents" class="head-container">
      <div v-if="crud.props.searchToggle" class="search-tools">
        <!-- 搜索 -->
        <el-button class="tools-all" size="small" type="primary" round>全部</el-button>
        <div class="tools-right">
          <template v-for="item in searchTools">
            <div v-if="item.menuType==='button' && item.pageUrl ==='group'" :key="item.id" class="tools-group">
              <span>{{ item.name }}</span>
              <div class="group-btns">
                <el-badge v-for="sub in item.children" :key="sub.id" :value="12" class="btn">
                  <el-button size="small" round @click="() =>{searchButton(sub)}">{{ sub.name }}</el-button>
                </el-badge>
                <!-- <el-button v-for="sub in item.children" :key="sub.id" round>{{ sub.name }}</el-button> -->
              </div>
            </div>
          </template>
        </div>
        <!-- <rrOperation :crud="crud"></rrOperation> -->
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <div slot="right">
          <el-input placeholder="请输入内容" style="width:150px;display:inline-block;" suffix-icon="el-icon-search"></el-input>
          <!-- <el-input placeholder="请选择日期" style="width:150px;display:inline-block;" suffix-icon="el-icon-date"></el-input> -->
          <el-date-picker style="width:150px;display:inline-block;" type="date" placeholder="选择日期" suffix-icon="el-icon-date"></el-date-picker>

          <rrOperation :crud="crud"></rrOperation>
        </div>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="ID">
            <el-input v-model="form.id" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="form.patientName" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio v-for="item in dict.gender" :key="item.id" v-model="form.gender" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="出生日期">
            <el-date-picker v-model="form.birthDay" type="datetime" style="width: 370px;"></el-date-picker>
          </el-form-item>
          <el-form-item label="家长姓名">
            <el-input v-model="form.parent" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="家长联系方式">
            <el-input v-model="form.parentMobile" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker v-model="form.createDate" type="datetime" style="width: 370px;"></el-date-picker>
          </el-form-item>
          <el-form-item label="主管医生ID，参考 t_doctor">
            <el-input v-model="form.doctorId" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="诊所ID">
            <el-input v-model="form.clinicId" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="所属部门">
            <el-input v-model="form.deptId" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="是否标星收藏，0未收藏，1已收藏">
            <el-input v-model="form.star" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="患者头像URL">
            <el-input v-model="form.photoUrl" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="产品规则">
            <el-input v-model="form.productRuleId" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="套餐规则">
            <el-input v-model="form.packageRuleId" style="width: 370px;"></el-input>
          </el-form-item>
          <el-form-item label="是否已归档 0 未归档 1 已归档 已归档普通查询不可见">
            <el-input v-model="form.archived" style="width: 370px;"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        stripe
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
        @cell-click="cellClick"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="photoUrl" width="120" sortable label="患者头像">
          <template slot-scope="scope">
            <img :src="scope.row.photoUrl" class="user-avatar" />
          </template>
        </el-table-column>
        <!--
        <el-table-column prop="patientName" width="150" sortable label="患者姓名"></el-table-column>
        <el-table-column prop="parentProfession" width="150" sortable label="诊疗阶段"></el-table-column>
        <el-table-column prop="parentProfession1" width="150" sortable label="套餐名称"></el-table-column>
        <el-table-column prop="parentProfession2" width="150" sortable label="治疗阶段"></el-table-column>
        <el-table-column prop="createDate" width="150" sortable label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="parentProfession3" width="150" sortable label="诊疗状态"></el-table-column>
        <el-table-column prop="parentProfession4" width="150" sortable label="描述"></el-table-column>
        <el-table-column prop="parentProfession5" width="150" sortable label="订单状态"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100px" align="center"> -->
        <el-table-column prop="patientName" sortable label="患者姓名">
          <template slot-scope="scope">
            <span style="color:#508fcc;">
              {{ scope.row.patientName }}
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="recordStat" sortable label="诊疗阶段" ></el-table-column> -->
        <el-table-column prop="packageName" sortable label="套餐名称"></el-table-column>
        <!-- <el-table-column prop="phase" sortable label="治疗阶段" ></el-table-column> -->
        <el-table-column prop="recordStat" sortable label="诊疗状态">
          <template slot-scope="scope">
            {{ dict.label.record_stat[scope.row.recordStat] }}
          </template>
        </el-table-column>
        <el-table-column prop="patientStat" sortable label="患者状态">
          <template slot-scope="scope">
            {{ dict.label.patient_stat[scope.row.patientStat] }}
          </template>
        </el-table-column>
        <el-table-column prop="createDate" sortable label="创建时间" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="phase" sortable label="诊疗阶段">
          <template slot-scope="scope">
            {{ dict.label.medical_phase[scope.row.phase] }}
          </template>
        </el-table-column>
        <el-table-column prop="logisticsStat" sortable label="订单状态" width="120"></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- <udOperation :data="scope.row" :permission="permission"></udOperation> -->
            <el-button type="primary" round @click="()=>{startVisit(scope.row)}">发起复诊</el-button>
            <el-button type="danger" round @click="()=>{finishData(scope.row)}">完善资料</el-button>
            <el-button type="danger" round @click="()=>{confirmMake(scope.row)}">确认制作</el-button>
            <el-button type="danger" round @click="()=>{mailModel(scope.row)}">邮寄牙膜</el-button>
            <el-button type="danger" round @click="()=>{confirmReceipt(scope.row)}">确认收货</el-button>
            <el-button type="primary" round @click="()=>{commitData(scope.row)}">提交资料</el-button>
            <el-button type="danger" round @click="()=>{stealth3D(scope.row)}">隐形3D方案</el-button>
            <!-- <el-button type="primary" round @click="lmReport">罗慕报告</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination></pagination>
    </div>
    <div v-else class="create" style="margin: -20px -20px -45px">
      <!-- 复诊添加用户信息 -->
      <AddPatient v-if="showComponents === 'add'" :params="editItem" @goBack="goBack"></AddPatient>
      <!-- 患者详情信息 -->
      <PatientDetails v-if="showComponents === 'visit'" :params="editItem" @goBack="goBack"></PatientDetails>
    </div>
    <el-dialog title="确认制作" :visible.sync="dialogVisible" width="800px">
      <ConfirmMake></ConfirmMake>
    </el-dialog>
    <el-dialog title="收货单" :visible.sync="receiveOrderVisible" width="800px">
      <ReceiveOrder>
        <el-button type="primary" round @click="receiveOrderVisible = false">确认收货</el-button>
      </ReceiveOrder>
    </el-dialog>
  </div>

</template>

<script>
import crudPatient from '@/api/patient';
import CRUD, { presenter, header, form, crud } from '@crud/crud';
import rrOperation from '@crud/RR.operation';
import crudOperation from '@crud/CRUD.operation';
// import udOperation from '@crud/UD.operation';
import pagination from '@crud/Pagination';
// import DateRangePicker from '@/components/DateRangePicker';
import { mapGetters } from 'vuex';
// import utils from '@/utils';
import AddPatient from '@/views/components/addPatient';
import PatientDetails from '@/views/components/patientDetails';
import ReceiveOrder from '@/views/components/receiveOrder';
import ConfirmMake from '@/views/docWork/ConfirmMake';

import { saleArea } from '@/api/order';
import {
  queryOper,
  queryPatient,
  queryOrg,
  queryDoctor
} from '@/api/operPatient';

const defaultForm = { id: null, patientName: null, gender: null, birthDay: null, compliance: null, parent: null, relationship: null, parentProfession: null, parentMobile: null, orthodonticHis: null, createDate: null, doctorId: null, clinicId: null, deptId: null, tags: null, star: null, photoUrl: null, productRuleId: null, packageRuleId: null, archived: null };
export default {
  name: 'Patient',
  components: { pagination, crudOperation, rrOperation, AddPatient, PatientDetails, ConfirmMake, ReceiveOrder },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: [
    'gender', // 性别
    'patient_parent_rel',
    'stl_flag', // 牙模类型
    'record_stat', // 诊疗状态
    'medical_phase', // 诊疗阶段
    'clinic_type', // 诊所性质
    'patient_stat', // 患者状态
    'report_result', // 报告诊断结果
    'report_stat' // 报告状态
  ],
  cruds() {
    return CRUD({
      title: 'PatientService',
      url: '/patient/queryPage',
      sort: 'id,desc',
      crudMethod: { ...crudPatient },
      props: { searchToggle: false },
      optShow: { add: false, download: false }
    });
  },
  data() {
    return {
      //   permission: {
      //     // add: ['admin', 'patient:add'],
      //     // edit: ['admin', 'patient:edit'],
      //     // del: ['admin', 'patient:del']
      //   },
      dialogVisible: false,
      receiveOrderVisible: false,
      rules: {
      },
      searchTools: [],
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'patientName', display_name: '姓名' },
        { key: 'gender', display_name: '性别' },
        { key: 'parent', display_name: '家长姓名' },
        { key: 'parentMobile', display_name: '家长联系方式' },
        { key: 'doctorId', display_name: '主管医生ID，参考 t_doctor' },
        { key: 'clinicId', display_name: '诊所ID' },
        { key: 'deptId', display_name: '所属部门' }
      ],
      showComponents: '',
      province: [], // 省
      city: [],
      packageNameList: [], // 套餐类型
      productNameList: [], // 矫治器类型
      // 模糊匹配初始化数据
      patientList: [], // 患者名字
      clinicList: [], // 诊所名称
      groupList: [], // 所属集团
      doctorList: [], // 主治医生
      // 模糊匹配动态数据
      patientNameList: [], // 患者名字
      clinicNameList: [], // 诊所名称
      groupNameList: [], // 所属集团
      doctorNameList: [], // 主治医生
      editItem: {}
    };
  },
  computed: {
    ...mapGetters([
      'menusMap',
      'autoPath',
      'menusCode',
      'codes',
      'searchInfo'
    ])
  },
  watch: {
    searchInfo(newVal, oldVal) {
      console.warn(newVal.code, '||', oldVal.code);
      if (newVal.code === 'doc:work:add') {
        this.showComponents = 'add';
      } else if (newVal.code === 'doc:work:visit') {
        this.showComponents = 'visit';
      }
    }
  },
  mounted() {
    this.searchTools = this.menusCode[this.codes.DOC_WORK] && this.menusCode[this.codes.DOC_WORK].children;
    this.initList();
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
    goBack() {
      this.showComponents = '';
      this.$store.dispatch('components/changeComponents', {
        id: 'SET_SIDEBAR', // 修改的显示的位置，可以不写
        isShow: false, // 是否显示
        components: 'Steps', // 组件类型
        Observer: { // 映射方法， 内部参数自定义，可以由组件内部发起进行跨域操作。
          refresh: () => {
            console.warn('Test Refresh !');
          }
        } // 组件调用数据方法
      });
    },
    searchButton(item) {
      console.warn(item.code, item.name);
    },
    initList() {
      // 患者名称
      queryPatient('queryPatientName', { patientName: '' })
        .then((res) => {
          this.patientList = res.map((item) => {
            item.label = item.patientName;
            return item;
          });
        })
        .catch((er) => {});
      // 诊所名称
      queryOrg('clinic/configList', { clinicName: '' })
        .then((res) => {
          this.clinicList = res.map((item) => {
            item.label = item.clinicName;
            return item;
          });
        })
        .catch((er) => {});
      // 所属集团
      queryOrg('group/configList', { groupName: '' })
        .then((res) => {
          this.groupList = res.map((item) => {
            item.label = item.groupName;
            return item;
          });
        })
        .catch((er) => {});
      // 主治医生
      queryDoctor('configList', { doctorName: '' })
        .then((res) => {
          this.doctorList = res.map((item) => {
            item.label = item.doctorName;
            return item;
          });
        })
        .catch((er) => {});
      // 省
      saleArea({ parentAreaId: '-1', sonlevel: '1' })
        .then((res) => {
          this.province = res;
        })
        .catch((er) => {});
      // 套餐类型
      queryOper('package/configList', { packageName: '' })
        .then((res) => {
          this.packageNameList = res;
        })
        .catch((er) => {});
      // 矫治器类型
      queryOper('product/configList', { productName: '' })
        .then((res) => {
          this.productNameList = res;
        })
        .catch((er) => {});
    },
    cellClick(row, type) {
      if (type.property === 'patientName') {
        this.showComponents = 'visit';
        this.editItem = row;
        this.$store.dispatch('components/changeComponents', {
          id: 'SET_SIDEBAR', // 修改的显示的位置，可以不写
          isShow: true, // 是否显示
          components: 'UserBaseInfo', // 组件类型
          data: row,
          Observer: { // 映射方法， 内部参数自定义，可以由组件内部发起进行跨域操作。
            refresh: () => {
              console.warn('Test Refresh !');
            }
          } // 组件调用数据方法
        });
      }
    },
    changeComponents(type) {
      this.showComponents = type;
    },
    // 发起复诊
    startVisit() {
      this.changeComponents('add');
    },
    // 完善资料
    finishData() {
      this.changeComponents('visit');
    },
    // 确认制作
    confirmMake() {

    },
    // 邮寄牙膜
    mailModel() {

    },
    // 确认收货
    confirmReceipt() {

    },
    // 提交资料
    commitData() {

    },
    // 3D方案
    stealth3D() {

    },
    // 罗慕报告
    lmReport() {

    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  .head-container {
    .search-tools {
      .tools-all {
        //   margin-top:10px;
        vertical-align: top;
      }
      .tools-right {
        display: inline-block;
        border-left: 1px solid #e6e6e6;
        padding-left: 10px;
        .tools-group {
          .group-btns {
            display: inline-block;
            margin-left: 30px;
            margin-bottom: 20px;
            .btn {
              margin-right: 30px;
            }
          }
        }
      }
    }
  }
}
</style>
